import { Shop } from '../shop/shop-model.js';
var shop = new Shop();
const app = getApp()
Page({

  data: {
    info:{
      order_num:0,
      total:0,
      shop:{
        address :'',
        logo:'',
        name: '', 
        score: 0
      }
    },
    totalUser:0,
    navHeight: app.globalData.navHeight,
    list: [],
    text: '确认',
    hiddenModal:true,
    show_type:1
  },

  onLoad: function (options) {
   
  },

  onReady: function () {

  },

  onShow: function () {
    this.info();
    this._getUser();
  },
  //基地信息
  info:function(){
    var args ={}
    shop.info(args, (res) => {
     this.setData({
       info:res.item
     })
    });
  },
  //扫描
  scan:function(){
    var that = this;
    // 允许从相机和相册扫码
    wx.scanCode({
      success(res) {
        if (res.result){
          that.getOrder();
        }
      }
    })
  },
  _getUser: function () {
    var that = this;
    var args = {
      page: 1,
      num: 1,
    }
    shop.user(args, (res) => {
      that.setData({
        totalUser: res._meta.totalCount
      })
    });
  },
  getOrder: function (check_num){
    var that = this;
    wx.showLoading({
      title: '查询订单...',
    })
    var params = {
      order_no: check_num
    }
    shop.scan(params, (data) => {
      if (data.items.length < 1) {
        wx.showModal({
          title: '提示',
          content: '没有找到该订单或该订单不属于当前基地',
          success(res) {
            if (res.confirm) {
              that.data.loading = false;
            } else if (res.cancel) {
              that.data.loading = false;
            }
          }
        })
        return false;
      }
      var items = [];
      var datas = data.items[0].orderSku;
      datas.forEach((item, index) => {
        items.push({ id: item.id, num: item.num, goods_name: item.goods_name, sku_value: item.sku_value, selected: true })
      })
      that.setData({
        hiddenModal: false,
        list: items,
        order_id: data.items[0].id
      })
    },
    (data) => {
      wx.hideLoading();
    });
  },
  sub: function () {
    var that = this;
    if (!this.data.order_id) {
      return false;
    }
    var params = { order_id: this.data.order_id }
    var origin = that.data.text;
    this.setData({
      text: '核验中...'
    })
    shop.checkout(params, (data) => {
      if (data.status == 1) {
        wx.showToast({
          title: '核验成功',
          icon: 'success',
          duration: 1500
        })
      } else {
        wx.showToast({
          title: data.msg,
          icon: 'none',
          duration: 3000
        })
      }
    },
    (data) => {
      that.data.loading = false;
      that.setData({
        text: origin,
        hiddenModal: true
      })
    });
  },
  hideModal: function (e) {
    this.setData({
      hiddenModal: true
    })
  },
  toLog: function () {
    wx.navigateTo({
      url: 'score-log?type=0',
    })
  },
  toMoney: function () {
    wx.navigateTo({
      url: 'money',
    })
  },
  toOrder: function () {
    wx.navigateTo({
      url: '/pages/shop_order/list',
    })
  },
  goto: function (e) {
    var url = e.currentTarget.dataset.url;
    wx.redirectTo({
      url: url,
    })
  },
  back: function () {
    wx.navigateBack();
  }
})